﻿using Mall.Infrastructure.Container;
using Sx.Mall.Common.Helper;
using Sx.Mall.WebContract;
using Sx.Mall.WebContract.Cache;
using Sx.Mall.WebContract.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Sx.Mall.Web.Filter
{
        /// <summary>
        /// 区域操作管理类
        /// </summary>
        public  class Regions
        {

           private static InterfaceContainer app = AppDomain.CurrentDomain.GetContainer();


            /// <summary>
            /// 获得全部区域
            /// </summary>
            /// <returns></returns>
            public static List<RegionInfoDto> GetAllRegion()
            {
                return Regions.GetAllRegion();
            }

            /// <summary>
            /// 获得省列表
            /// </summary>
            /// <returns></returns>
            public static List<RegionInfoDto> GetProvinceList()
            {
                List<RegionInfoDto> provinceList = MallCache.Get(CacheKeys.SHOP_REGION_CHILDLIST + 0) as List<RegionInfoDto>;
                if (provinceList == null)
                {
                    provinceList = GetRegionList(0);
                    MallCache.Insert(CacheKeys.SHOP_REGION_CHILDLIST + 0, provinceList);
                }
                return provinceList;
            }

            /// <summary>
            /// 获得市列表
            /// </summary>
            /// <param name="provinceId">省id</param>
            /// <returns></returns>
            public static List<RegionInfoDto> GetCityList(int provinceId)
            {
                List<RegionInfoDto> cityList = MallCache.Get(CacheKeys.SHOP_REGION_CHILDLIST + provinceId) as List<RegionInfoDto>;
                if (cityList == null)
                {
                    cityList = GetRegionList(provinceId);
                    MallCache.Insert(CacheKeys.SHOP_REGION_CHILDLIST + provinceId, cityList);
                }
                return cityList;
            }

            /// <summary>
            /// 获得县或区列表
            /// </summary>
            /// <param name="cityId">市id</param>
            /// <returns></returns>
            public static List<RegionInfoDto> GetCountyList(int cityId)
            {
                List<RegionInfoDto> countyList = MallCache.Get(CacheKeys.SHOP_REGION_CHILDLIST + cityId) as List<RegionInfoDto>;
                if (countyList == null)
                {
                    countyList = GetRegionList(cityId);
                    MallCache.Insert(CacheKeys.SHOP_REGION_CHILDLIST + cityId, countyList);
                }
                return countyList;
            }

            /// <summary>
            /// 获得区域列表
            /// </summary>
            /// <param name="parentId">父id</param>
            /// <returns></returns>
            public static List<RegionInfoDto> GetRegionList(int parentId)
            {
                return Regions.GetRegionList(parentId);
            }

            /// <summary>
            /// 获得区域
            /// </summary>
            /// <param name="regionId">区域id</param>
            /// <returns></returns>
            public static RegionInfoDto GetRegionById(int regionId)
            {
                if (regionId > 0)
                {
                    RegionInfoDto regionInfo = MallCache.Get(CacheKeys.SHOP_REGION_INFOBYID + regionId) as RegionInfoDto;
                    if (regionInfo == null)
                    {
                        regionInfo = Regions.GetRegionById(regionId);
                        MallCache.Insert(CacheKeys.SHOP_REGION_INFOBYID + regionId, regionInfo);
                    }
                    return regionInfo;
                }
                return null;
            }

            /// <summary>
            /// 获得区域
            /// </summary>
            /// <param name="name">名称</param>
            /// <param name="layer">级别</param>
            /// <returns></returns>
            public static RegionInfoDto GetRegionByNameAndLayer(string name, int layer)
            {
                RegionInfoDto regionInfo = MallCache.Get(string.Format(CacheKeys.SHOP_REGION_INFOBYNAMEANDLAYER, name, layer)) as RegionInfoDto;
                if (regionInfo == null)
                {
                    regionInfo = Regions.GetRegionByNameAndLayer(name, layer);
                    MallCache.Insert(string.Format(CacheKeys.SHOP_REGION_INFOBYNAMEANDLAYER, name, layer), regionInfo);
                }
                return regionInfo;
            }

            /// <summary>
            /// 获取IP对应区域
            /// </summary>
            /// <param name="ip">ip</param>
            /// <returns></returns>
            public static RegionInfoDto GetRegionByIP(string ip)
            {
                RegionInfoDto regionInfo = null;
                HttpCookie cookie = HttpContext.Current.Request.Cookies["loc"];
                IRegionsService service = app.GetSerivce<IRegionsService>();
                if (cookie != null)
                {
                    if (cookie["ip"] == ip)
                    {
                        regionInfo = GetRegionById(TypeHelper.StringToInt(cookie["regionid"]));
                    }
                    else
                    {
                        cookie.Values["ip"] = ip;
                        regionInfo = service.SearchRegion(ip);
                        if (regionInfo != null)
                            cookie.Values["regionid"] = regionInfo.RegionId.ToString();
                        else
                            cookie.Values["regionid"] = "-1";
                        cookie.Expires = DateTime.Now.AddYears(1);

                        HttpContext.Current.Response.AppendCookie(cookie);
                    }

                }
                else
                {
                    cookie = new HttpCookie("loc");
                    cookie.Values["ip"] = ip;
                    regionInfo = service.SearchRegion(ip);
                    if (regionInfo != null)
                        cookie.Values["regionid"] = regionInfo.RegionId.ToString();
                    else
                        cookie.Values["regionid"] = "-1";
                    cookie.Expires = DateTime.Now.AddYears(1);

                    HttpContext.Current.Response.AppendCookie(cookie);
                }

                if (regionInfo != null)
                    return regionInfo;
                else
                    return new RegionInfoDto() { RegionId = -1, Name = "未知区域" };
            }
    }
}